import random

def genRTOData():
  data = {
    'DE-BFO': {},
    'FCFS': {},
    'PSO': {},
    'EPSO': {},
  }
  steps = range(50, 501, 50)
  for s in steps:
    print(s)
  trials = 100
  for sys in data.keys():
    for numJob in steps:
      data[sys][numJob] = []
      for job in range(numJob):
        if sys == 'DE-BFO':
          avg = 50 + job / 20
          r = avg + random.randint(-int(avg * 0.2), int(avg * 0.5))
        if sys == 'FCFS':
          avg = 60 + job / 20
          r = avg + random.randint(-int(avg * 0.25), int(avg * 0.65))
        if sys == 'PSO':
          avg = 70 + job / 20
          r = avg + random.randint(-int(avg * 0.4), int(avg * 0.4))
        if sys == 'EPSO':
          avg = 80 + job / 20
          r = avg + random.randint(-int(avg * 0.50), int(avg * 0.4))
        data[sys][numJob].append(r)
      
  with open('dr.csv', 'w') as f:
    f.write('sys,numJob,job,time\n')
    for sys in data.keys():
      for numJob, sysData in data[sys].items():
        jid = 0
        for d in sysData:
          arr = [sys, str(numJob), str(jid), str(d)]
          s = ','.join(arr)
          f.write(s + '\n')
          jid += 1
      
  with open('sr.csv', 'w') as f:
    f.write('sys,numJob,job,time\n')
    for sys in data.keys():
      for numJob, sysData in data[sys].items():
        jid = 0
        for d in sysData:
          arr = [sys, str(numJob), str(jid), str(d + random.randint(1,4))]
          s = ','.join(arr)
          f.write(s + '\n')
          jid += 1
      
  with open('sche.csv', 'w') as f:
    f.write('sys,numJob,job,type,time\n')
    for numJob, numJobData in data['DE-BFO'].items():
      sortedData = sorted(numJobData)
      size = len(sortedData)
      lower = random.sample(sortedData[0:int(size/5 * 3)], int(size /2))
      upper = random.sample(sortedData[int(size/5 * 2):], int(size/2))
      for d in lower:
        arr = ['DE-BFO', str(numJob), str(jid), 'SSRV', str(d + random.randint(1,4))]
        s = ','.join(arr)
        f.write(s + '\n')
      for d in upper:
        arr = ['DE-BFO', str(numJob), str(jid), 'DSRV', str(d + random.randint(1,4))]
        s = ','.join(arr)
        f.write(s + '\n')
  
genRTOData()